package com.sss.common.util;

import com.sss.common.util.enums.ResponseDataEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sss
 * @date 2019-09-17 15:20
 **/
public class ResponseDataHelper<T> {

    private Integer code;

    private String message;

    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResponseDataHelper(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseDataHelper(ResponseDataEnum responseDataEnum) {
        this.code=responseDataEnum.getCode();
        this.message=responseDataEnum.getMessage();
    }

    public static<T> ResponseDataHelper<T> succes(T data){
        ResponseDataHelper dataHelper = new ResponseDataHelper(ResponseDataEnum.SUCCESS);
        dataHelper.setData(data);
        return  dataHelper;
    }


    public static final Integer SUCCESS_CODE=200;

    public static final Integer FAIL_CODE=-1;

    public static final String SUCCESS_MSG="ok";

    public static final String FAIL_MSG="error";

    public static Map<String,Object> getAllBaseMap(String msg,Integer code ,Object data){
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("msg",msg);
        map.put("code",code);
        map.put("data",data);
        return map;
    }

    public static Map<String, Object> success(Object data){
      return   getAllBaseMap(SUCCESS_MSG,SUCCESS_CODE,data);
    }

    public static Map<String, Object> success(){
      return   getAllBaseMap(SUCCESS_MSG,SUCCESS_CODE,new HashMap(16));
    }

    public static Map<String, Object> fail(){
        return   getAllBaseMap(FAIL_MSG,FAIL_CODE,new HashMap(16));
    }

    public static Map<String, Object> fail(Object data){
        return   getAllBaseMap(FAIL_MSG,FAIL_CODE,data);
    }

    public static Map<String, Object> fail(Integer code,String msg){
        return   getAllBaseMap(msg,code,new HashMap(16));
    }

    public static Map<String, Object> fail(String msg){
        return   getAllBaseMap(msg,FAIL_CODE,new HashMap(16));
    }

}
